package com.eclink.asset.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.eclink.asset.asset.domain.dto.AssetInventoryOperateDTO;
import com.eclink.asset.convert.AssetInventoryConvert;
import com.eclink.asset.asset.domain.dao.AssetInventoryRelItem;
import com.eclink.asset.domain.query.AssetInventoryQuery;
import com.eclink.asset.enums.AssetInventoryItemResultEnum;
import com.eclink.asset.enums.AssetInventoryStateEnum;
import com.eclink.asset.mapper.AssetInventoryRelItemMapper;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 资产分类表Service业务层处理
 *
 *
 * @author zcc
 */
@Service
public class AssetInventoryDetailService {

    @Resource
    private AssetInventoryRelItemMapper mapper;

    @Resource
    private AssetInventoryRelItemMapper assetInventoryRelItemMapper;

    @Resource
    private AssetInventoryConvert convert;

    /**
     * 查询资产盘点的清单
     *
     * @param query 查询条件
     * @param page 分页条件
     * @return 资产分类表
     */
    public List<AssetInventoryRelItem> selectListDetailItem(AssetInventoryQuery query, Pageable page, String parentId) {
        if (page != null) {
            PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize());
        }
        LambdaUpdateWrapper<AssetInventoryRelItem> qw = new LambdaUpdateWrapper<>();
        qw.eq(AssetInventoryRelItem::getParentId, parentId);
//        List<Integer> stateList = new ArrayList<>();
//        stateList.add(AssetInventoryStateEnum.DRAFT.getValue());
//        stateList.add(AssetInventoryStateEnum.NORMAL.getValue());
//        stateList.add(AssetInventoryStateEnum.LOSS.getValue());
//        qw.in(AssetInventoryRelItem::getState, stateList);
        qw.orderByDesc(AssetInventoryRelItem::getId);
        return mapper.selectList(qw);
    }

    public void inventory(String[] itemIds) {
        for(String id: itemIds) {
            AssetInventoryRelItem item = mapper.selectById(id);
            item.setState(AssetInventoryItemResultEnum.NORMAL.getValue());
            mapper.updateById(item);
        }
    }

    public AssetInventoryRelItem getRelItemByRFID(String rfid) {
        QueryWrapper<AssetInventoryRelItem> qw = new QueryWrapper<>();
        qw.eq("rfid",rfid);
        return assetInventoryRelItemMapper.selectOne(qw);
    }

    public void inventory(AssetInventoryOperateDTO dto) {
        Map<String,Object> formData = dto.getFormData();
        Integer result = (Integer) formData.get("result");
        List<AssetInventoryRelItem> list = dto.getItems();
        for(AssetInventoryRelItem item : list){
            LambdaUpdateWrapper<AssetInventoryRelItem> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(AssetInventoryRelItem::getImgUrlInventory, item.getImgUrlInventory());
            updateWrapper.set(AssetInventoryRelItem::getFormData, JSONObject.toJSONString(formData));
            if (result != null) {
                updateWrapper.set(AssetInventoryRelItem::getState, result);
            }
            updateWrapper.eq(AssetInventoryRelItem::getId, item.getId());
            assetInventoryRelItemMapper.update(null, updateWrapper);
        }
    }

    public AssetInventoryRelItem getProfitDetail(Long id) {
        return assetInventoryRelItemMapper.selectById(id);
    }

    public void addProfit(AssetInventoryRelItem item) {
        item.setState(AssetInventoryStateEnum.PROFIT.getValue());
        assetInventoryRelItemMapper.insert(item);
    }

    public void updateProfit(AssetInventoryRelItem item) {
        assetInventoryRelItemMapper.updateById(item);
    }

    public void delProfit(Long[] ids) {
        for (Long id : ids) {
            assetInventoryRelItemMapper.deleteById(id);
        }

    }

    public void autoInventory(List<String> rfidList) {
        for(String rfid: rfidList) {
            AssetInventoryRelItem item = getRelItemByRFID(rfid);
            if (item!=null) {
                item.setState(AssetInventoryItemResultEnum.NORMAL.getValue());
                mapper.updateById(item);
            }

        }
    }

    public void finishRelItem(Long id) {
        AssetInventoryRelItem item = mapper.selectById(id);
        item.setState(AssetInventoryItemResultEnum.NORMAL.getValue());
        mapper.updateById(item);
    }

}
